package com.ruoyi.orderdetails.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.orderdetails.mapper.OrderdetailsMapper;
import com.ruoyi.orderdetails.domain.Orderdetails;
import com.ruoyi.orderdetails.service.IOrderdetailsService;

/**
 * 订单细节Service业务层处理
 * 
 * @author sqlstudent
 * @date 2025-06-13
 */
@Service
public class OrderdetailsServiceImpl implements IOrderdetailsService 
{
    @Autowired
    private OrderdetailsMapper orderdetailsMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate; // 注入Redis模板

    private static final String CACHE_NAME = "orderdetails";
    /**
     * 查询订单细节
     * 
     * @param orderDetailId 订单细节主键
     * @return 订单细节
     */
    @Override
    @Cacheable(value = CACHE_NAME, key = "#orderDetailId")
    public Orderdetails selectOrderdetailsByOrderDetailId(Long orderDetailId)
    {
        return orderdetailsMapper.selectOrderdetailsByOrderDetailId(orderDetailId);
    }

    /**
     * 查询订单细节列表
     * 
     * @param orderdetails 订单细节
     * @return 订单细节
     */
    @Override
    @Cacheable(value = CACHE_NAME, key = "#orderdetails.cacheKey()")
    public List<Orderdetails> selectOrderdetailsList(Orderdetails orderdetails)
    {
        return orderdetailsMapper.selectOrderdetailsList(orderdetails);
    }

    /**
     * 新增订单细节
     * 
     * @param orderdetails 订单细节
     * @return 结果
     */
    @Override
    @CacheEvict(value = CACHE_NAME, allEntries = true)
    public int insertOrderdetails(Orderdetails orderdetails)
    {
        return orderdetailsMapper.insertOrderdetails(orderdetails);
    }

    /**
     * 修改订单细节
     * 
     * @param orderdetails 订单细节
     * @return 结果
     */
    @Override
    @CacheEvict(value = CACHE_NAME, key = "#orderdetails.orderDetailId") // 清除单个缓存
    public int updateOrderdetails(Orderdetails orderdetails)
    {
        return orderdetailsMapper.updateOrderdetails(orderdetails);
    }

    /**
     * 批量删除订单细节
     * 
     * @param orderDetailIds 需要删除的订单细节主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = CACHE_NAME, allEntries = true)
    public int deleteOrderdetailsByOrderDetailIds(Long[] orderDetailIds)
    {
        for (Long id : orderDetailIds) {
            redisTemplate.delete(CACHE_NAME + "::" + id);
        }
        return orderdetailsMapper.deleteOrderdetailsByOrderDetailIds(orderDetailIds);
    }

    /**
     * 删除订单细节信息
     * 
     * @param orderDetailId 订单细节主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = CACHE_NAME, key = "#orderDetailId")
    public int deleteOrderdetailsByOrderDetailId(Long orderDetailId)
    {
        return orderdetailsMapper.deleteOrderdetailsByOrderDetailId(orderDetailId);
    }
}
